import requests
import openpyxl
import json
import hashlib
import hmac
import time

def sign(params, srt):
    signParams = []
    for i in params:
        middle = i + params[i]
        signParams.append(middle)
    signParams.sort()
    str = ""
    signStr = bytes(str.join(signParams), encoding="utf8")
    srt = bytes(srt, encoding="utf8")
    print(hmac.new(srt, signStr, digestmod=hashlib.sha1).hexdigest())
    print(signStr, srt)
    #用过私钥对字符串进行 hmac_sha1，得到签名
    return (hmac.new(srt, signStr, digestmod=hashlib.sha1).hexdigest()).upper()

#excel name
excel_name = './cartest.xlsx'
wb=openpyxl.load_workbook(excel_name)
sheet=wb.worksheets[0]
sheet['A1'].value = '请求车牌号'
sheet['B1'].value = '请求地址'
sheet['C1'].value = '请求参数'
sheet['D1'].value = '响应结果'
sheet['E1'].value = '测试时间'

#test api
url = 'https://testopen.95155.com/save/apis/vQueryLicenseV2'
#pro api
url = 'https://zhiyunopenapi.95155.com/save/apis/vQueryLicenseV2'
#6b4764ce
token = '####' 
cid = '7568391b-dd31-49f2-82fd-516cfc972592'
srt = '2e6738d9-5a6a-4169-b8f7-1dbef2b3dfa8'
#车牌号
vclN = '蒙B93345' 
vclN = '蒙KG6801' 
vclN = '蒙AX3495' 
vclN = '蒙B90083'
#车牌颜色
vco = "2"
data = {
    "token": token,
    "cid": cid,
    "vclN": vclN,
    "vco": vco,
    "sign": ''
}

#car_num
car_num = [
    '蒙B93345',
    '蒙KG6801',
    '蒙AX3495',
    '蒙B90083',
    '蒙L65839',
    '宁AG8200',
    '晋B92014',
    '晋BT4157',
    '蒙K65333',
    '晋BN6591'
]
i = 2
for num in car_num:
    time.sleep(2)
    data['vclN'] = num
    data.pop('sign')
    print(data, 222)
    data['sign'] = sign(data,srt)
    print(data)
    requests.packages.urllib3.disable_warnings()
    r = requests.post(url, params = data, verify = False)
    #开始处理写入测试方案
    sheet['A'+str(i)] = num
    sheet['B'+str(i)] = url
    sheet['C'+str(i)] = json.dumps(data)
    sheet['D'+str(i)] = r.text
    sheet['E'+str(i)] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    i=i+1

wb.save(excel_name)


